<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\model;
use \temmoku\db;
class module {
	
	public function add($data=[]){
		$id = db::insert(jab."module")->cols(['name'=>$data['name']])->query();
		db::query("CREATE TABLE  `".jab."content_{$id}` (
					  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
					  `aid` mediumint(9) NULL DEFAULT 0,
					  PRIMARY KEY (`id`),
					  INDEX `aid` (`aid`)
					) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");
	}
	public function alllist(){
		$data=db::select('*')->from(jab."module")->orderByDESC(['reorder'])->query();
		return $data;
	}
	public function del($id=0){
		//把本模型下的字段全部删除
		db::delete(jab.'field')->where('mid='.$id)->query();
		//把本模型下的字段数据全部删除
		db::delete(jab.'attribute_list')->where('mid='.$id)->query();
		//查询是否存在表
		$result = db::query("SHOW TABLES LIKE '".jab."content_$id'");
		if($result){//存在在删除
			db::query("DROP TABLE ".jab."content_$id");
		}
		//删除模型分类
		db::delete(jab."module")->where("id=$id")->query();
		//改变一下信息模型值,要不然无法修改信息数据,会爆数据库错误
		db::update(jab.'article')->cols(['mid'=>0])->where('mid='.$id)->query();
		//改变一下分类绑定的模型
		db::update(jab.'category')->cols(['cmid'=>0])->where('cmid='.$id)->query();
		$this->del_html($id);//删除发布模板
	}
	
	public function field_test($mid=0){
		$field="field_".rand('1000','9999');
		if(db::query("Describe ".jab."content_$mid"." $field")){
			$this->field_test($mid);
		}
		return $field;
	}
	
	public function make_html($mid=0){
		
		$dir=Temmoku_PATH."view/home/default/home/";
		$_device=['pc','wap','wechat'];
		$_file=['articles.tpl','category.tpl'];
		$data=db::select('*')->from(jab."field")->where("mid = '$mid'")->orderByDESC(['reorder'])->query();
		foreach($_device AS $device){
			foreach($_file AS $file){
				$_dir=$dir.$device."/".$file;
				if(is_file($_dir)){
					if($file=='articles.tpl'){
						$text=fils_content($_dir);
						$html='	
								<div class="module_more">
									<table class="layui-table my-tabel">
										<tbody>';
						foreach($data AS $key=>$row){
							$html.='
											<tr>
												<td width="100">'.$row['name'].'</td>
												<td>{{$data[\''.$row['field'].'\']}}</td>
											</tr>
							';
						}
						$html.='
										</tbody>
									</table>
								</div>';
						
						$text=str_ireplace('{{$module_more}}',$html,$text);
						$olddir=$dir.$device."/articles_$mid.tpl";
						write_file($olddir,$text);
					}else{
						$olddir=$dir.$device."/category_$mid.tpl";
						copy  ( $_dir  , $olddir );
					}
				}
				
			}
		}
	}
	private function del_html($mid=0){
		$dir=Temmoku_PATH."view/home/default/home/";
		$_device=['pc','wap','wechat'];
		$_file=['articles_'.$mid.'.tpl','category_'.$mid.'.tpl'];
		foreach($_device AS $device){
			foreach($_file AS $file){
				$_dir=$dir.$device."/".$file;
				if(is_file($_dir)){
					unlink ($_dir);
				}
			}
		}
	}
}
?>